package com.xuxin.pda.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ActivityUtils;
import com.lxj.xpopup.XPopup;
import com.xuxin.pda.R;
import com.xuxin.pda.activity.SplashActivity;
import com.xuxin.pda.base.BaseConstant;
import com.xuxin.pda.base.BaseFragment;
import com.xuxin.pda.popup.ConfirmPopup;
import com.xuxin.pda.utils.ACache;

import butterknife.BindView;
import butterknife.OnClick;

public class UserFragment extends BaseFragment {
    @BindView(R.id.user_name)
    TextView user_name;
    @BindView(R.id.user_no)
    TextView user_no;
    @BindView(R.id.button)
    Button button;

    public static UserFragment newInstance() {
        return new UserFragment();
    }

    @Override
    public View setContentView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        return inflater.inflate(R.layout.fragment_user, container, false);
    }

    @Override
    public void initView() {
        button.setText("退出登录");
    }

    @Override
    public void initData() {
        ACache aCache = ACache.get(context);
        String userId = aCache.getAsString(BaseConstant.userId);
        String userName = aCache.getAsString(BaseConstant.userName);
        String userNo = aCache.getAsString(BaseConstant.userNo);
        user_name.setText(userName);
        user_no.setText(userNo);
    }

    @Override
    public void onComplete() {
    }

    @Override
    @OnClick({R.id.button})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.button) {
            new XPopup.Builder(context)
                    .dismissOnTouchOutside(true)
                    .dismissOnBackPressed(true)
                    .asCustom(new ConfirmPopup(context, "确定要退出此账号吗？", new ConfirmPopup.OnConfirmListener() {
                        @Override
                        public void onConfirm() {
                            ACache.get(context).clear();
                            Intent intent = new Intent(context, SplashActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                            ActivityUtils.finishAllActivities();
                            ActivityUtils.startActivity(intent);
                        }
                    })).show();
        }
    }
}